#!/usr/bin/env python
# -*- coding:utf-8 -*-

# src/grujieclient/systray.py

# Copyright (C) 2009 Gong Han <gong AT fedoraproject.org>

# This file is part of gRuijieClient.

# gRuijieClient is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.

# gRuijieClient is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with gRuijieClient. If not, see <http://www.gnu.org/licenses/>.

import pynotify
from globe import *

class TrayNotify(pynotify.Notification):
    """
    GTK notify message class
    @author: Gong Han
    """
    def __init__(self, pwidget):
        super(TrayNotify, self).__init__('gruijieclient')
        self.set_timeout(5000)
        self.attach_to_status_icon(pwidget);
    def put_msg(self, title, msg):
        self.update(title, msg)
        self.show();

class SysTray(gtk.StatusIcon):
    """
    GTK system tray class
    @author: Gong Han
    """
    def __init__(self, pwidget):
        gtk.StatusIcon.__init__(self)
        # load tray's icon file
        try:
            self.set_from_file(GRUIJIECLIENT_IMAGE_DISCON)
        except:
            print >> sys.stderr, 'can not load icon'
        self.set_tooltip('gRuijieClient - unconnected')
        # get popup menu from glade
        self._popmenu = GladeUI.get_widget('menu_systray')
        # connect menu signals
        self.connect('popup-menu', self.on_icon_right_clicked)
        self.connect('activate', self.on_icon_left_clicked)
        # show system tray
        self.set_visible(True)
        # attach libnotify to systray
        try:
            pynotify.init("gruijieclient")
        except:
            print >> sys.stderr, 'traynotify init error'
            sys.exit(1)
        self.traynotify = TrayNotify(self)
        # initialize flag of tray status
        self._systray_flag = True
        # parent widget which tray is attached to.
        self._pwidget = pwidget

    def __del__(self):
        try:
            pynotify.uninit("gruijieclient")
        except:
            pass

    def on_icon_right_clicked(self, widget, button,
                              activate_time=None):
        # popup tray menu
        self._popmenu.popup(parent_menu_shell=None,
                           parent_menu_item=None,
                           func=None, button=0,
                           activate_time=activate_time)

    def on_icon_left_clicked(self, widget=None):
        # determine whether main window should be shown or not
        if self._systray_flag:
            self._pwidget.hide_all()
        else:
            self._pwidget.show_all()
        # update tray status flag
        self._systray_flag = not self._systray_flag

if __name__ == '__main__':
    print TrayNotify
    print SysTray